<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once ('include/conn.php');
require_once ('system.class.php');
function messagepagelist($args, $content, &$smarty){

	/* 定义全局变量pageinfo,用于生成静态栏目页时使用
	 * 由于limit是在模板中指定的,而在生成栏目静态页时
	* 需要根据limit判断需要生成静态页的数量,故从此处
	* 获取。
	* */
    
    
       // $info = array ('article_num' => 100, 'pageno'=>$args['page']);
        
       // $smarty->assign("info",$info);
        
      //  return $content;
        
        
        
        
        
	$pageinfo=array();
	//$channel_id=$args['channel_id'];
        $page_id=$args['page'];
	$limit=$args['limit'];
	if($limit<0){
		$limit=4;
	}
	$pageinfo['limit']=$limit;
	$offset=$args['offset'];
	if($offset<0){
		$offset=0;
	}
	$sql_get_message_number="select * from guest_book ORDER BY id DESC";

	/*获取文章个数*/
	$result_get_message_number=mysql_query($sql_get_message_number) or die(mysql_error());
	$numrows=mysql_num_rows($result_get_message_number);
	if($offset>$numrows){
		$offset=($numrows%$limit==0)?($numrows-$limit):($numrows-($numrows%$limit));
	}
	if($offset%$limit!=0){
		$offset=$offset-$offset%$limit;
	}
	$pageinfo['message_num']=$numrows;
		$info['url']=CIE."/message.php?page=$page_id";
	
	

	/*获取文章信息*/
	$sql_get_message_information="select id,nickname,content,createtime,reply,replytime from guest_book order by id desc limit $offset,$limit";
	$result_get_message_information=mysql_query($sql_get_message_information) or die(mysql_error());
	/*显示查询结果*/
	while ($data=mysql_fetch_row($result_get_message_information)) {
		$message_id=$data[0];
		$arr[$message_id]['id']=$data[0];
		$arr[$message_id]['nickname']=$data[1];
		$arr[$message_id]['content'] = $data[2];
		$arr[$message_id]['createtime']=date('Y-m-d',$data[3]);
		$arr[$message_id]['reply']=$data[4];
		$arr[$message_id]['replytime']=date('Y-m-d',$data[5]);	
	}
	/*文章数*/
	$info['message_num']=$numrows;
	/*首页*/
	$link=CIE."/message.php?offset=0&page=$page_id";
	$info['message_home']="<span class='page'><a href='$link'>首页</a></span>";
	/*上一页*/
	$info['pre_page']="";
	if ($offset >0) {
		$prevoffset=$offset-$limit;
		if($prevoffset>=0){
			$link=CIE."/message.php?offset=$prevoffset&page_id=$page_id";
			$info['pre_page']="<span class='page'><a href='$link'>上一页</a></span>";
		}
	}
	/*计算页面数*/
	$pages=intval($numrows/$limit);

	/*如果有余数，加一*/
	if ($numrows%$limit) {
		$pages++;
	}
	/*显示页数*/
	$info['pages']=null;
	$current_num=$offset/$limit+1;
	$i=($current_num>3)?($current_num-2):1;
	$limit_num=$i+5;
	while($i<=$pages&&$i<$limit_num) {
		$newoffset=$limit*($i-1);
		$page_num=$i;
		if($i==$current_num){
			$page_num="<span class='current_page'>$i</span>";
		}
		$link=CIE."/message.php?offset=$newoffset&page_id=$page_id";
		$info['pages'][$i]="<span class='page'><a href='$link'>$page_num</a></span>";
		$i++;
	}

	/*下一页*/
	$info['next_page']="";
	if (!(($offset/$limit)==$pages) && $pages!=1) {
		// not last page so give NEXT link
		$newoffset=$offset+$limit;
		if($newoffset<$numrows){
			$link=CIE."/message.php?offset=$newoffset&page_id=$page_id";
			$info['next_page']="<span class='page'><a href='$link'>下一页</a></span>";
		}
	}
	/*尾页*/
	$newoffset=($numrows%$limit)?($numrows-$numrows%$limit):($numrows-$numrows%$limit-$limit);
	if($newoffset<0){
		$newoffset=0;
	}
	$link=CIE."/message.php?offset=$newoffset&page_id=$page_id";
	$info['last_page']="<span class='page'><a href='$link'>尾页</a></span>";
	if(empty($info)){
		$infor=null;
	}
	if(empty($arr)){
		$arr=null;
	}
	$GLOBALS['pageinfo']=$pageinfo;
	$smarty->assign("list",$arr);
	$smarty->assign("info",$info);
	return $content;
}
?>
